﻿<%@ WebHandler Language="C#" Class="pagination" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Chenxin.Lib.House;

public class pagination : AjaxHandlerBase
{

    override public void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string type = "";//获取分页类型
        //（table:房源管理插入层房源详情页跟单分页分页，trace:房源详情页跟单分页，view:房源详情页带看分页，comm:楼盘管理插入层楼栋分页，房源分配房源分页，allot：分配房源层分页）
        int pageindex = 0;//页码
        int PageSize = 0;//页显示行数
        string OrderField = "";//排序
        string StrWhere = "";//条件 
        string FieldList = "";//欲选择字段列表 
        string TableFieldList = "";//子查询欲选择字段列表 
        string TableName = "";//表名
        string houseid = "";//获取房源编号

        //获得结果集
        DataSet ds = new DataSet();

        //获取房源编号
        if (!string.IsNullOrEmpty(context.Request.QueryString["houseid"]))
        {
            houseid = context.Request.QueryString["houseid"];
        }

        //获取分页类型
        if (!string.IsNullOrEmpty(context.Request.QueryString["type"]))
        {
            type = context.Request.QueryString["type"];
        }
        //页码        
        if (!string.IsNullOrEmpty(context.Request.QueryString["pageIndex"]))
        {
            pageindex = int.Parse(context.Request.QueryString["pageIndex"]) + 1;
        }
        //页显示行数
        if (!string.IsNullOrEmpty(context.Request.QueryString["pageRow"]))
        {
            PageSize = int.Parse(context.Request.QueryString["pageRow"]);
        }


        /**
         *修改人：熊世宏
         *时间：  2010-03-30
         *备注：以后参数统一，所有字母小写，页号:pageindex,页大小：pagesize
        */
        //页码        
        if (!string.IsNullOrEmpty(context.Request.QueryString["pageindex"]))
        {
            pageindex = int.Parse(context.Request.QueryString["pageindex"]) + 1;
        }
        //页显示行数
        if (!string.IsNullOrEmpty(context.Request.QueryString["pagesize"]))
        {
            PageSize = int.Parse(context.Request.QueryString["pagesize"]);
        }
        if (type == "table" || type == "trace")
        {
            string status = context.Request.QueryString["status"];
            OrderField = "House_TraceInfo.TraceTime desc";
            StrWhere = "HouseId='" + houseid + "'";
            if (!string.IsNullOrEmpty(status))
            {
                StrWhere += " and status = '" + status + "' ";
            }
            FieldList = "Tractor,TraceTime,TraceContext,Depart";
            TableFieldList = "Tractor,CONVERT(varchar(12) , TraceTime, 111 ) as TraceTime,TraceContext,Depart";
            TableName = "House_TraceInfo";
        }
        else if (type == "view")
        {
            OrderField = "House_TraceInfo.TraceTime desc";
            TableName = "";
            StrWhere = "HouseId='" + houseid + "'";
            FieldList = "Tractor,TraceTime,TraceContext,Depart";
            TableFieldList = "Tractor,CONVERT(varchar(12) , TraceTime, 111 ) as TraceTime,TraceContext,Depart";
            TableName = "House_TraceInfo";
        }
        else if (type == "comm")
        {
            PageSize = 4;
            OrderField = "Community_BuildingInfo.AddTime desc";
            StrWhere = "CommunityId='" + houseid + "'";
            FieldList = "BuildingId,BuildingName,Floor,UnitNum,AddPerson,AddTime";
            TableFieldList = "BuildingId,BuildingName,Floor,UnitNum,AddPerson,CONVERT(varchar(12) , AddTime, 111 ) as AddTime";
            TableName = "Community_BuildingInfo";
        }
        else if (type == "allot")
        {
            OrderField = "House_BaseInfo.Adddate desc";
            StrWhere = "OprPersonId='" + houseid + "'";
            FieldList = "HouseId,Adddate,HouseCode,Community,OprPerson,OprDepart";
            TableFieldList = "HouseId,CONVERT(varchar(12) , Adddate, 111 ) as Adddate,HouseCode,isnull(Community,'')+isnull(BuildingName,'')+isnull(Room,'') as Community,OprPerson,OprDepart";
            TableName = "House_BaseInfo";
        }
        else if (type == "allot_custom")
        {
            OrderField = "Customer_BaseInfo.Adddate desc";
            StrWhere = "OprPersonId='" + houseid + "'";
            FieldList = "CustomerId,Adddate,CustomerCode,CustomerName,OprPerson,OprDepart";
            TableFieldList = "CustomerId,CONVERT(varchar(12) , Adddate, 111 ) as Adddate,CustomerCode,CustomerName,OprPerson,OprDepart";
            TableName = "Customer_BaseInfo";
        }
        else if (type == "ShowGenDan")
        {
            string status = context.Request.QueryString["status"];
            OrderField = "House_TraceInfo.TraceTime desc";
            StrWhere = "HouseId='" + houseid + "'";
            //if (!string.IsNullOrEmpty(status))
            //{
            //    StrWhere += " and status = '" + status + "' ";
            //}
            FieldList = "Tractor,TraceTime,TraceContext,Depart";
            TableFieldList = "Tractor,CONVERT(varchar(12) , TraceTime, 111 ) as TraceTime,TraceContext,Depart";
            TableName = "House_TraceInfo";
        }
        else if (type == "ShowWeiTou")
        {
            string status = context.Request.QueryString["status"];
            OrderField = "House_EntrustInfo.InputTime desc";
            StrWhere = "HouseId='" + houseid + "'";
            //if (!string.IsNullOrEmpty(status))
            //{
            //    StrWhere += " and Status = '" + status + "' ";
            //}
            FieldList = "Employee,EmpDepart,EntrustType,StartTime,EndTime,[Type],EntrustCode";
            TableFieldList = "Employee,EmpDepart,EntrustType,convert(nvarchar(12),StartTime,111) as StartTime,convert(nvarchar(12),EndTime,111) as EndTime,case when Type='10' then '出售' when Type='20' then '出租' else '租售' end as [Type],EntrustCode";
            TableName = "House_EntrustInfo";
        }
        else if (type == "ShowBeiAn")
        {
            string status = context.Request.QueryString["status"];
            OrderField = "House_MarkedInfo.AddTime desc";
            StrWhere = "HouseId='" + houseid + "'";
            //if (!string.IsNullOrEmpty(status))
            //{
            //    StrWhere += " and Status = '" + status + "' ";
            //}
            FieldList = "MarkPerson,MarkDepart,MarkDay,MarkCode,MarkStartTime,MarkEndTime";
            TableFieldList = "MarkPerson,MarkDepart,convert(nvarchar(10),MarkDay,120) as MarkDay,MarkCode,convert(nvarchar(12),MarkStartTime,111) as MarkStartTime,convert(nvarchar(12),MarkEndTime,111) as MarkEndTime";
            TableName = "House_MarkedInfo";
        }
        else if (type == "ShowYaoShi")
        {
            string status = context.Request.QueryString["status"];
            OrderField = "House_KeyUsedInfo.AddTime desc";
            StrWhere = "ReceiptionId=KeyReceiptionId and HouseId='" + houseid + "'";
            //if (!string.IsNullOrEmpty(status))
            //{
            //    StrWhere += " and Status = '" + status + "' ";
            //}
            FieldList = "UsePersonName,InOrOut,ActionDay";
            TableFieldList = "UsePersonName,case when InOrOut=10 then '借出' else '归还' end as InOrOut,convert(nvarchar(10),ActionDay,120) as ActionDay";
            TableName = "House_KeyUsedInfo,House_KeyReceiptionInfo";
        }
        else if (type == "ShowFenPei")
        {
            string status = context.Request.QueryString["status"];
            OrderField = "Tasks_BaseInfo.Addtime desc";
            StrWhere = "relatedId='" + houseid + "'";
            //if (!string.IsNullOrEmpty(status))
            //{
            //    StrWhere += " and Status = '" + status + "' ";
            //}
            FieldList = "Title,taskReceiver,ReceiverDepart,TaskType,TaskBeginDay,TaskEndDay";
            TableFieldList = "Title,taskReceiver,ReceiverDepart,TaskType,convert(nvarchar(10),TaskBeginDay,120) as TaskBeginDay,convert(nvarchar(10),TaskEndDay,120) as TaskEndDay";
            TableName = "Tasks_BaseInfo";
        }

        //根据sql执行查询
        Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HOUSE);



        //设置缓存KEY（分页类型+房源ID）
        /*
        string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.HOUSE, type + "." + houseid);
        
        //从缓存中获取分页数据（只包含第一页并且不包括分配）
        if (pageindex == 1 && type != "allot")
        {
            ds = cs.GetDataSet(strCacheKey);

            if (!cs.Hit)//若缓存未命中，则从数据库中获取并存入缓存，时间为5分钟
            {
                ds = Chenxin.Lib.House.DAL.House_TraceInfo.GetList(pageindex, PageSize, OrderField, TableName, StrWhere, FieldList, TableFieldList, true);
                cs.Set(strCacheKey, ds,DateTime.Now.AddMinutes(10));
            }
        }
            
        else*/
        ds = Chenxin.Lib.House.DAL.House_TraceInfo.GetList(pageindex, PageSize, OrderField, TableName, StrWhere, FieldList, TableFieldList, true);


        //DataSet ds = Chenxin.Lib.House.DAL.House_TraceInfo.GetList(pageindex, PageSize, OrderField, TableName, StrWhere, FieldList, TableFieldList, true);


        StringBuilder sb = new StringBuilder();

        //sb.Append("<input type='hidden' id='tdiv_count' value='" + ds.Tables[1].Rows[0][0] + "' />");
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string td = "";
            if (type == "table")
                td = String.Format("<tr><td><span class='fc_blue'>{0}</span></td><td><span class='fc_blue'>{1}</span></td><td title='{2}'><span class='fc_blue fc_wdhid'>{3}</span></td></tr>", ds.Tables[0].Rows[i][0], ds.Tables[0].Rows[i][1], ds.Tables[0].Rows[i][2].ToString().Replace("\\n", " "), ds.Tables[0].Rows[i][2].ToString().Replace("\\n", " "));
            else if (type == "trace" || type == "view")
                td = String.Format("<tr class='tab_line5'><td width='68'>{0}/{3}</td><td width='96'>{1}</td><td width='544'>{2}</td></tr>", ds.Tables[0].Rows[i][0], ds.Tables[0].Rows[i][1], ds.Tables[0].Rows[i][2], ds.Tables[0].Rows[i][3]);//javascript:editbuild('{4}');javascript:delbuild('{5}');
            else if (type == "comm")
                td = String.Format("<tr><td><span class='fc_blue'>{0}</span></td><td><span class='fc_blue'>{1}</span></td><td><span class='fc_blue'>{2}</span></td><td><span class='fc_blue'>{3}</span></td><td><span class='fc_blue'><a href=\"# \" name=\"btn_building_add{4}\">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#\" name=\"btn_building_del{5}\">删除</a></span></td></tr>", ds.Tables[0].Rows[i][1], ds.Tables[0].Rows[i][2], ds.Tables[0].Rows[i][4], ds.Tables[0].Rows[i][5], ds.Tables[0].Rows[i][0], ds.Tables[0].Rows[i][0]);
            else if (type == "allot"||type=="allot_custom")
                td = String.Format("<tr><td><input type='checkbox' name='allotck' id='{0}'/></td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>", ds.Tables[0].Rows[i][0], ds.Tables[0].Rows[i][1], ds.Tables[0].Rows[i][2], ds.Tables[0].Rows[i][3], ds.Tables[0].Rows[i][4], ds.Tables[0].Rows[i][5]);
            sb.Append(td);
        }
        if (type == "ShowGenDan")
        {
            context.Response.Write(string.Format("{{success:'1',data:{{data:'{0}',count:{1}}}}}", CreateGrid(type, ds.Tables[0].Select("1=1")), Convert.ToInt32(ds.Tables[1].Rows[0].ItemArray[0])));
            return;
        }
        if (type == "ShowWeiTou")
        {
            context.Response.Write(string.Format("{{success:'1',data:{{data:'{0}',count:{1}}}}}", CreateGrid(type, ds.Tables[0].Select("1=1")), Convert.ToInt32(ds.Tables[1].Rows[0].ItemArray[0])));
            return;
        }
        if (type == "ShowBeiAn")
        {
            context.Response.Write(string.Format("{{success:'1',data:{{data:'{0}',count:{1}}}}}", CreateGrid(type, ds.Tables[0].Select("1=1")), Convert.ToInt32(ds.Tables[1].Rows[0].ItemArray[0])));
            return;
        }
        if (type == "ShowYaoShi")
        {
            context.Response.Write(string.Format("{{success:'1',data:{{data:'{0}',count:{1}}}}}", CreateGrid(type, ds.Tables[0].Select("1=1")), Convert.ToInt32(ds.Tables[1].Rows[0].ItemArray[0])));
            return;
        }
        if (type == "ShowFenPei")
        {
            context.Response.Write(string.Format("{{success:'1',data:{{data:'{0}',count:{1}}}}}", CreateGrid(type, ds.Tables[0].Select("1=1")), Convert.ToInt32(ds.Tables[1].Rows[0].ItemArray[0])));
            return;
        }
        context.Response.Write(sb.ToString());
    }



    /// <summary>
    /// 生成列表
    /// </summary>
    /// <param name="drList"></param>
    /// <returns></returns>
    public string CreateGrid(string type, DataRow[] drList)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        if (type == "ShowGenDan")
        {
            sb.Append("<table id=\"showGenDan\" width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><thead><tr class=\"tab_line4\"><td width=\"25%\">");
            sb.Append("<b>跟盘人</b></td><td width=\"20%\"><b>跟盘时间</b></td><td width=\"52%\"><b>跟盘内容</b></td></tr>");
            foreach (DataRow dr in drList)
            {
                sb.AppendFormat("<tr class=\"tab_line5\"><td  title=\"{0}/{3}\">{0}/{3}</td><td  title=\"{1}\">{1}</td><td title=\"{2}\">{2}</td></tr>", dr[0], dr[1], dr[2], dr[3]);

            }
            sb.Append("</thead></table>");
        }
        else if (type == "ShowWeiTou")
        {
            sb.Append("<table id=\"showWeiTou\" width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><thead>");
            sb.Append("<tr class=\"tab_line4\"><td width=\"30%\"><b>委托人</b></td><td width=\"10%\"><b>委托类型</b></td><td width=\"20%\"><b>有效期</b></td></td><td width=\"10%\"><b>委托性质</b></td><td width=\"22%\"><b>委托编号</b></td></tr>");
            foreach (DataRow dr in drList)
            {
                sb.AppendFormat("<tr class=\"tab_line5\"><td  title=\"{0}/{1}\">{0}/{1}</td><td  title=\"{2}\">{2}</td><td  title=\"{3}-{4}\">{3}-{4}</td><td  title=\"{5}\">{5}</td><td  title=\"{6}\">{6}</td></tr>", dr[0], dr[1], dr[2], dr[3], dr[4], dr[5], dr[6]);
            }
            sb.Append("</thead></table>");
        }
        else if (type == "ShowBeiAn")
        {
            sb.Append("<table id=\"showBeiAn\" width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><thead>");
            sb.Append("<tr class=\"tab_line4\"><td width=\"20%\"><b>备案人</b></td><td width=\"20%\"><b>备案时间</b></td><td width=\"20%\"><b>备案号</b></td></td><td width=\"20%\"><b>备案开始时间</b></td><td width=\"20%\"><b>备案开始时间</b></td></tr>");
            foreach (DataRow dr in drList)
            {
                sb.AppendFormat("<tr class=\"tab_line5\"><td title=\"{0}/{1}\">{0}/{1}</td><td title=\"{2}\">{2}</td><td title=\"{3}\">{3}</td><td title=\"{4}\">{4}</td><td title=\"{5}\">{5}</td></tr>", dr[0], dr[1], dr[2], dr[3], dr[4], dr[5]);
            }
            sb.Append("</thead></table>");
        }
        else if (type == "ShowYaoShi")
        {
            sb.Append("<table id=\"showYaoShi\" width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><thead>");
            sb.Append("<tr class=\"tab_line4\"><td width=\"30%\"><b>使用人</b></td><td width=\"30%\"><b>使用类型</b></td><td width=\"40%\"><b>使用时间</b></td></tr>");
            foreach (DataRow dr in drList)
            {
                sb.AppendFormat("<tr class=\"tab_line5\"><td title=\"{0}\">{0}</td><td  title=\"{1}\">{1}</td><td title=\"{2}\">{2}</td></tr>", dr[0], dr[1], dr[2]);
            }
            sb.Append("</thead></table>");
        }
        else if (type == "ShowFenPei")
        {
            sb.Append("<table id=\"showFenPei\" width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><thead>");
            sb.Append("<tr class=\"tab_line4\"><td width=\"30%\"><b>主题</b></td><td width=\"20%\"><b>接收人</b></td><td width=\"10%\"><b>任务类型</b></td><td width=\"20%\"><b>开始时间</b></td><td width=\"20%\"><b>结束时间</b></td></tr>");
            foreach (DataRow dr in drList)
            {
                sb.AppendFormat("<tr class=\"tab_line5\"><td title=\"{0}\">{0}</td><td title=\"{1}/{2}\">{1}/{2}</td><td title=\"{3}\">{3}</td><td title=\"{4}\">{4}</td><td title=\"{5}\">{5}</td></tr>", dr[0], dr[1], dr[2], dr[3], dr[4], dr[5]);
            }
            sb.Append("</thead></table>");
        }
        return sb.ToString();
    }
}